<template>
  <!--查看答辩成绩-->
  <div class="view-reply-result">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" />
    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <!--查看老师/学生详情弹框-->
      <TeacherStuInfoDialog :viewDetailData="viewDetailData" :dialogVisible="dialogVisible" :viewType="viewType" @taacher-stu-close="closeDialog"></TeacherStuInfoDialog>

      <condition-card @reset="reset" @search="searchList" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.graduationYear" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.adminCollege')">
              <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange" class="search-item" clearable>
                <el-option v-for="item in source.studentColleges" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.major')">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.replyTopic')">
              <el-input v-model="queryForm.projectName" :placeholder="$t('graduationDesign.pleaseEnter')" class="search-item" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.replyGroup')">
              <el-input v-model="queryForm.defenseGroupName" :placeholder="$t('graduationDesign.pleaseEnter')" class="search-item" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.replyResult')">
              <el-select v-model="queryForm.defenseResult" clearable class="search-item" filterable>
                <el-option v-for="item in source.resultTypeOptions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.status')">
              <el-select v-model="queryForm.passStatus" filterable class="search-item" clearable>
                <el-option v-for="item in source.statusOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card>
        <div class="edu-page-title">{{$t('graduationDesign.replyResult')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-left">
            <el-input v-model="queryForm.studentName" :placeholder="$t('graduationDesign.pleaseEnterStudentNameAndTeacherName')" style="width:300px" @keyup.enter.native="searchList" clearable>
              <i slot="suffix" class="el-icon-search" style="margin-top:10px;cursor:pointer" @click="searchList"></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
          </div>
        </div>

        <edu-table ref="assignReplyStuTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="graduationYear" min-width="80"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.student')" prop="studentName" min-width="130"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyTopic')" prop="projectName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.tutor')" prop="tutorName" min-width="130"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyLocation')" prop="defenseLocation" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.startTime')" prop="defenseStartTime" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyGroupLeader')" prop="defenseGroupLeaderName" min-width="130"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyGroup')" prop="defenseGroupName" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyResult')" min-width="90">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.defenseResult === 0" type="success">{{$t('graduationDesign.excellent')}}</el-tag>
              <el-tag v-else-if="scope.row.defenseResult === 1" type="info">{{$t('graduationDesign.good')}}</el-tag>
              <el-tag v-else-if="scope.row.defenseResult === 2" type="info">{{$t('graduationDesign.medium')}}</el-tag>
              <el-tag v-else-if="scope.row.defenseResult === 3" type="info">{{$t('graduationDesign.passing')}}</el-tag>
              <el-tag v-else-if="scope.row.defenseResult === 4" type="danger">{{$t('graduationDesign.failed')}}</el-tag>
              <el-tag v-else-if="scope.row.defenseResult === 5" type="info">{{$t('graduationDesign.invalid')}}</el-tag>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.status')" min-width="100">
            <template slot-scope="scope">
              <el-tag v-if="RESULT_LIST.includes(scope.row.defenseResult)" type="success">{{$t('graduationDesign.pass')}}</el-tag>
              <el-tag v-else-if="scope.row.defenseResult === 4" type="danger">{{$t('graduationDesign.fail')}}</el-tag>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="90">
            <template slot-scope="scope">
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>
    <!--查看-->
    <div v-if="indexKey === 'VIEW'">
      <ReplyAssessmentInfo :replyAssessmentForm="replyAssessmentForm"></ReplyAssessmentInfo>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/assignReplyStudent";
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'assignReplyStudent',
  data() {
    return {
      api: API,
      getList: '/api/designservice/defenseGroup/queryDefenseResult',
      queryForm: {
        graduationYear: '',
        collegeId: '',
        subjectId: '',
        projectName: '',
        defenseGroupName: '',
        defenseResult: '',
        passStatus: '',
        activeAuthId: localStorage.getItem("activeAuthId")
      },
      indexKey: 'MAIN',
      source: {
        studentColleges: [], // 行政学院
        collegeOptions: [], // 学院
        subjectOptions: [], // 专业
        resultTypeOptions: [{
          label: '优',
          value: 0
        }, {
          label: '良',
          value: 1
        }, {
          label: '中',
          value: 2
        }, {
          label: '及格',
          value: 3
        }, {
          label: '不及格',
          value: 4
        }, {
          label: '无效',
          value: 5
        }],
        // 状态
        statusOptions: [{
          label: "通过",
          value: true
        }, {
          label: "不通过",
          value: false
        }]
      },
      selectedData: [], // 勾选的列表数据
      breadPath: {
        label: this.$t('graduationDesign.viewReplyResult'),
        key: "MAIN",
        children: [
          {
            key: "VIEW",
            label: this.$t('graduationDesign.view')
          }
        ]
      },
      loading: false,
      dialogVisible: false,
      viewDetailData: [], // 查看教师学生列表
      viewType: '',
      replyAssessmentForm: {},
      RESULT_LIST: [0, 1, 2, 3]
    };
  },
  mounted() {
    this.queryForm.graduationYear = new Date().getFullYear() + '';
    this.getCollege();
    this.searchList();
    tools.initAllCollege(this.source.studentColleges);
  },
  methods: {
    searchList() {
      this.$refs.assignReplyStuTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        graduationYear: '',
        collegeId: '',
        subjectId: '',
        projectName: '',
        defenseGroupName: '',
        defenseResult: '',
        passStatus: '',
        activeAuthId: localStorage.getItem("activeAuthId")
      };
      this.source.subjectOptions = [];
      this.queryForm.graduationYear = new Date().getFullYear() + '';
      this.searchList();
    },
    handleSelectChange(val) {
      this.selectedData = val;
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
    },
    // 根据毕业年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.graduationYear) {
        const params = {
          grade: this.queryForm.graduationYear,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    // 查看
    onView(row) {
      this.api.getEvaluateInfo(row.studentId, row.graduationYear).then(({ data }) => {
        this.indexKey = "VIEW";
        this.replyAssessmentForm = Object.assign({}, { ...row }, { ...data });
      });
    },
    // 关闭
    closeDialog() {
      this.dialogVisible = false;
    },
    // 导出
    onExport() {
      let studentIdList = [];
      this.selectedData.forEach(item => {
        studentIdList.push(item.studentId);
      });
      const params = {
        condition: this.queryForm,
        studentList: studentIdList
      };
      this.api.resultExport(params).then(res => {
        if (res.code == 200) {
          // this.delayRequest(res.data.key);
          tools.downloadFile(res.data.key, "查看答辩成绩.xls", null, this);
        }
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      this.api.getExportStatus(key).then(res => {
        this.loading = false;
        if (res.code === 200 && res.data.status) {
          let path = 'api/designservice/common/export?path=' + res.data.path;
          window.open(path);
        } else {
          this.delayRequest(key);
        }
      }).catch(err => {
        this.loading = false;
        this.delayRequest(key);
      });
    }
  },
  components: {
    ReplyAssessmentInfo: () => import('./components/replyAssessmentInfo'),
    TeacherStuInfoDialog: () => import('../assignReplyStudent/components/teacherStuInfoDialog')
  }
}
</script>
<style lang="scss" scoped>
.view-reply-result {
    .search-item {
        width: 100%;
    }
}
</style>
